//
//  MyMusicViewController.m
//  哎游
//
//  Created by 康守帅 on 15-1-14.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "MyMusicViewController.h"
#import "macro.h"
#import "MusicModel.h"
#import "MusicDataBase.h"
#import "MusicDetailTableViewCell.h"
#import "MusicPlayerViewController.h"
#import "Singleton.h"

@interface MyMusicViewController ()<UITableViewDelegate, UITableViewDataSource>

@end

@implementation MyMusicViewController

- (void)dealloc
{
    [_index release];
    [_timer release];
    [_tableArray release];
    [_tableView release];
    [super dealloc];
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.tableArray = [NSMutableArray array];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 25, 25)];
    [button setBackgroundImage:[UIImage imageNamed:@"return.png"] forState:UIControlStateNormal];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
    [button addTarget:self action:@selector(returnBack) forControlEvents:UIControlEventTouchUpInside];

    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(100, 0, 45, 100)];
    label.font = [UIFont fontWithName:@"TimesNewRomanPS-BoldMT" size:20];
    label.text = @"我的音乐";
    label.textColor = [UIColor whiteColor];
    self.navigationItem.titleView = label;
    [self.tableArray removeAllObjects];
    
    [button release];
    [label release];
    [self selectMusic];
    [self createTableView];
}

- (void)selectMusic
{
    Singleton *share = [Singleton shareInstance];
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    [self.tableArray removeAllObjects];
    if (share.loginState == SinaLogin) {
        [_tableArray addObjectsFromArray:[MusicDataBase selectWithUserMusic:[user objectForKey:@"userSina"]]];
    } else {
       [_tableArray addObjectsFromArray:[MusicDataBase selectWithUserMusic:[user objectForKey:@"userName"]]]; 
    }
}

- (void)viewWillAppear:(BOOL)animated
{
    [self selectMusic];
    [self.tableView reloadData];
}

- (void)createTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [_tableView registerClass:[MusicDetailTableViewCell class] forCellReuseIdentifier:@"cell"];
//    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableView];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_tableArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MusicDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    MusicModel *model = [_tableArray objectAtIndex:indexPath.row];
    cell.title.text = model.title;
    cell.author.text = model.nickname;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    self.index = indexPath;
    self.timer = [NSTimer scheduledTimerWithTimeInterval:2.0f target:self selector:@selector(cancelSelectCellState) userInfo:nil repeats:YES];
    Singleton *share = [Singleton shareInstance];
    MusicModel *model = [_tableArray objectAtIndex:indexPath.row];
    share.musicPlayerViewController.musicArray = self.tableArray;
    if ([share.musicId isEqualToString:[model.trackId stringValue]]) {
        share.musicPlayerViewController.sameMusic = YES;
    }else {
        share.musicPlayerViewController.progressSlider.value = 0;
        share.musicPlayerViewController.currentTimeLabel.text = nil;
        share.musicPlayerViewController.totalTimeLabel.text = nil;
        share.musicPlayerViewController.name.text = nil;
        share.musicPlayerViewController.sameMusic = NO;
        share.musicPlayerViewController.imageUrl = nil;
        share.musicId = [model.trackId stringValue];
        share.musicPlayerViewController.imageUrl = model.imageUrl;
    }
    [self presentViewController:share.musicPlayerViewController animated:YES completion:nil];
}

- (void)cancelSelectCellState
{
    [self.tableView deselectRowAtIndexPath:self.index animated:NO];
    [self.timer invalidate];
}

- (void) returnBack
{
    [self.navigationController popToRootViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
